<?php
/**
 * Created by JetBrains PhpStorm.
 * User: miaodb
 * Date: 11-10-27
 * Time: 下午4:35
 * To change this template use File | Settings | File Templates.
 */
 
class CLI_Updatecredit extends CLI
{


    /**
     * 脚本主方法
     *
     * @return void
     */
    public function main()
    {
        $select = DB::select()->from("members");
        $iterator = new Db_Iterator($select, 'default', 10);
        $i = 0;
        while ($iterator->valid())
        {
            // 遍历数据
			foreach ($iterator->current() as $member)
            {
                $nickname = $member['nickname'];
                $member_id = $member['member_id'];
                $shop_id = DB::select('shop_id')->from('shops')->where('member_id','=',$member_id)->fetch_one();
                $good_num = DB::select(DB::expr('count(*)'))
                        ->from('tc_rate')
                        ->where('rated_nick','=',$nickname)
                        ->where('result','=',1)
                        ->where('deleted','=','0')
                        ->fetch_one();
                $medium_num = DB::select(DB::expr('count(*)'))
                        ->from('tc_rate')
                        ->where('rated_nick','=',$nickname)
                        ->where('result','=',0)
                        ->where('deleted','=','0')
                        ->fetch_one();
                $bad_num = DB::select(DB::expr('count(*)'))
                        ->from('tc_rate')
                        ->where('rated_nick','=',$nickname)
                        ->where('result','=',-1)
                        ->where('deleted','=','0')
                        ->fetch_one();
                if($member['good_num']!=$good_num or $member['medium_num']!=$medium_num or $member['bad_num']!=$bad_num)
                {
                    $credit = $good_num - $bad_num;
                    if($credit<0)$credit = 0;
                    $update = array('good_num'=>$good_num, 'medium_num'=>$medium_num,'bad_num'=>$bad_num,'credit_score'=>$credit);
                    DB::update('members')->set($update)->where('member_id','=',$member_id)->execute();
                }

                if($shop_id)
                {
                    $times = $good_num+$medium_num+$bad_num;
                    $old_times = DB::select(DB::expr('count(*)'))->from('tc_shop_score')->where('shop_id','=',$shop_id)->fetch_one();
                    if($old_times>$times)
                    {
                        $limit = $old_times - $times;
                        mysql_query("delete from tc_shop_score where shop_id={$shop_id} limit {$limit}");
                        
                    }
                    elseif($old_times<$times)
                    {
                        $limit = $times - $old_times;
                        for($i = 0; $i<$limit;$i++)
                        {
                            $insert = array(
                                'shop_id'=>$shop_id,
                                'des_score'=>5,
                                'service_score'=>5,
                                'express_score'=>5,
                                'created'=>time(),
                                'deleted'=>0,
                            );
                            DB::insert('tc_shop_score', array_keys($insert))->values(array_values($insert))->execute();
                        }
                    }
                }


            }
            echo '>';
                $iterator->next();
        }
        die;

        }

    }